<?php
/**
 * 应用自定义视图模板类
 * 
 * @version $Id: AppView.php 685 2013-02-21 09:15:19Z linsir123 $
 */
class AppView
{
	/**
	 * @var 相对路径
	 */
	static $domain;
	
	/**
	 * @var Css集合
	 */
	static $css;
	
	/**
	 * @var Javascript集合
	 */
	static $js;
	
	/**
	 * 初始化
	 */
	static function init()
	{
		static $__init = false;
		if ($__init) {
			return ;
		}
		$__init = true;
		
		///
		$view = App::getView();
		$engine = & $view->engine;
		
		self::$css = array('themes/css/layout%d.css', 'themes/01/_global.css');
		self::$js = array('jquery-1.6.2.min.js', 'jquery.base64.js', '_e35go/global.js');
		self::$domain = App::getRequest()->domain;
		
		/// 动态模块
		$engine->registerPlugin('block', 'dynamic', 		array('AppView', 'dynamic'), false);
		
		/// 函数
		$engine->registerPlugin('function', 'JS', 			array('AppView', 'sDomJs'));
		$engine->registerPlugin('function', 'CSS', 			array('AppView', 'sDomCss'));
		$engine->registerPlugin('function', 'shopScore', 	array('AppView', 'sShopScore'));
		$engine->registerPlugin('function', 'itemInfo', 	array('AppView', 'sItemInfo'));
		
		/* 链接相关 */
		$engine->registerPlugin('function', 'urlItem', 		array('Route', 'urlItem'));
		$engine->registerPlugin('function', 'urlItemJump', 	array('Route', 'urlItemJump'));
		$engine->registerPlugin('function', 'urlShop', 		array('Route', 'urlShop'));
		$engine->registerPlugin('function', 'urlShopJump', 	array('Route', 'urlShopJump'));
		$engine->registerPlugin('function', 'urlClass', 	array('Route', 'urlClass'));
		$engine->registerPlugin('function', 'urlZhe', 		array('Route', 'urlZhe'));
		$engine->registerPlugin('function', 'urlShops',		array('Route', 'urlShops'));
		$engine->registerPlugin('function', 'urlPhoto',		array('Route', 'urlPhoto'));
		
		/* 导航相关 */
		$engine->registerPlugin('function', 'navTop', 			array('Nav', 'top'));
		$engine->registerPlugin('function', 'navHeader', 		array('Nav', 'header'));
		$engine->registerPlugin('function', 'navSub', 			array('Nav', 'sub'));
	}
	
	/**
	 * 动态内容
	 */
	static function dynamic($param, $content)
	{
		return $content;
	}
	
	/**
	 * 添加Css
	 */
	static function addCss($css)
	{
		$args = func_get_args();
		
		///
		foreach ($args as $v) 
			array_push(self::$css, $v);
	}
	
	/**
	 * 添加Javascript
	 */
	static function addJs($js)
	{
		$args = func_get_args();
		
		///
		foreach ($args as $v) 
			array_push(self::$js, $v);
	}
	
	/**
	 * 店铺积分
	 */
	static function sShopScore($params = null)
	{
		$score = floatval($params['score']);
		$score = intval($score*10);
		for ($i=50; $i>0; $i-=5) {
			if ($score >= $i) {
				$score = $i;
				break;
			}
		}
		
		return '<img src="' . self::$domain.'images/score/'.$score.'.gif" width="60" height="12" />&nbsp;'.$params['score'];
	}
	
	/**
	 * 商品信息明细
	 * // 区分IE6
	 */
	static function sItemInfo($params = null)
	{
		$desc = trim($params['desc']);
		
		///
		$desc = preg_replace_callback("/(src=[\"|\']([^\"\']+)[\"|\'])/i", array("AppView", "sItemInfoPic"), $desc);
		
		///
		return preg_replace("/(?<=href=)([^\>]*)(?=\>)/i", "#", $desc);
	}
	
	/**
	 * 图片加载
	 */
	static function sItemInfoPic($matches)
	{
		///
		return sprintf('class="tbpItem" src="%s" tbpItem="%s"',
			self::$domain ."images/blank.png", 
			base64_encode($matches[2]));
	}
	
	/**
	 * Css
	 */
	static function sDomCss($params = null)
	{
		$layout = intval($params['layout']);
		if (empty($layout)) 
			$layout = 2;
			
		self::$css[0] = sprintf(self::$css[0], $layout);
		
		///
		if (strlen(self::$domain)==1) 
			$b = '';
		else {
			$b = substr(self::$domain, 1);
			$b = substr($b, 0, strlen($b)-1);
			$b = 'b='.$b.'&amp;';
		}
		$content = '<link type="text/css" rel="stylesheet" href="';
		$content .= self::$domain.'min/'.$b.'f='.implode(',', self::$css);
		$content .= '?v=20130118" />';
		
		///
		return $content;
	}
	
	/**
	 * Javascript
	 */
	static function sDomJs($params = null)
	{
		$content .= '<script type="text/javascript" src="';
		$content .= self::$domain.'min/b='.substr(self::$domain, 1).'js&amp;f='.implode(',', self::$js);
		$content .= '"></script>';
		
		///
		return $content;
	}
}
